//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.woniuxy.user.web.controller;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.user.dao.UserAccount;
import com.woniuxy.user.dao.UserInfo;
import com.woniuxy.user.model.dto.UserLoginDto;
import com.woniuxy.user.model.form.AddUserForm;
import com.woniuxy.user.model.form.UserLogin1Form;
import com.woniuxy.user.model.form.UserLoginForm;
import com.woniuxy.user.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(
        tags = {"用户账号相关接口"}
)
@RequestMapping({"/userAccount"})
public class UserAccountController {
    @Autowired
    private UserAccountService userAccountService;

    public UserAccountController() {
    }

    @ApiOperation("用户注册")
    @PostMapping({"/register"})
    private Result addUser(@Validated @RequestBody AddUserForm form ,BindingResult br) throws Exception {
        if (br.hasErrors()) {
            // 如果有验证错误发生，可以获取所有错误消息
            StringBuilder errorMessage = new StringBuilder();
            br.getAllErrors().forEach(error -> {
                errorMessage.append(error.getDefaultMessage()).append("!");
            });
            return Result.getResult(Boolean.class).setMessage(errorMessage.toString()).setData(false);
        }
        this.userAccountService.register(form);
        return Result.getResult().setMessage("注册成功");
    }

    @ApiOperation("用户注册发送短信")
    @GetMapping({"/send"})
    @ApiImplicitParam(name = "userInfoTel",value = "用户电话号码")
    private Result send(String userInfoTel) throws Exception {
        this.userAccountService.send(userInfoTel);
        return Result.getResult().setMessage("用户注册发送短信成功");
    }

    @ApiOperation("用户手机号登录发送短信")
    @GetMapping({"/send1"})
    @ApiImplicitParam(name = "userInfoTel",value = "用户电话号码")
    private Result send1(String userInfoTel) throws Exception {
        System.out.println("----");
        System.out.println(userInfoTel);
        this.userAccountService.send1(userInfoTel);
        return Result.getResult().setMessage("用户手机号登录发送短信成功");
    }

    @ApiOperation("用户账号密码登录")
    @PostMapping({"/login"})
    private Result<UserLoginDto> login(@RequestBody UserLoginForm uf, HttpServletResponse response) throws Exception {
        UserLoginDto userLoginDto = this.userAccountService.login(uf);
        System.out.println("33333333333333333333"+userLoginDto.toString());
        HashMap<String, Object> map = new HashMap();
        map.put("id", userLoginDto.getUserId());
        String token = JwtUtil.creatUserToken(map);
        response.setHeader("token", token);
        System.out.println(token);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return Result.getResult(UserLoginDto.class).setMessage("用户账户密码登录成功");
    }

    @ApiOperation("用户手机验证码登录")
    @PostMapping({"/login1"})
    private Result<UserLoginDto> login1(@RequestBody UserLogin1Form uf, HttpServletResponse response) throws Exception {
        UserLoginDto userLoginDto = this.userAccountService.login1(uf);
        HashMap<String, Object> map = new HashMap();
        map.put("id", userLoginDto.getUserId());
        String token = JwtUtil.creatUserToken(map);
        response.setHeader("token", token);
        System.out.println(token);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return Result.getResult(UserLoginDto.class).setMessage("用户手机验证码登录成功");
    }

    @ApiOperation("平台修改用户状态")
    @GetMapping("/updateUserState")
    public Result updateUserState(@RequestHeader String token){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        UserAccount userAccount= userAccountService.updateUserState(Integer.valueOf(id));
        return Result.getResult(UserAccount.class).setMessage("修改用户状态成功").setData(userAccount);
    }

    @ApiOperation("用户修改密码")
    @GetMapping("/userupdatePassword")
    @ApiImplicitParams({@ApiImplicitParam(name = "oldPass",value = "旧密码"),@ApiImplicitParam(name = "newPass",value = "新密码")})
    public Result userupdatePassword(String oldPass, String newPass,@RequestHeader String token){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        userAccountService.userupdatePassword(oldPass,newPass,id);
        return Result.getResult().setMessage("用户修改密码成功");
    }
}
